package com.hzhk.happyrent.ui.activity.personal;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.Constants;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.CodeModel;
import com.hzhk.happyrent.bean.person.ServiceBean;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.view.ClearEditText;
import com.hzhk.happyrent.utils.FileUtil;
import com.hzhk.happyrent.utils.MyPerference;
import com.hzhk.happyrent.utils.glide.ShowImageUtils;
import com.tamic.novate.Throwable;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CallCenterActivity extends AppCompatActivity {

    @BindView(R.id.activity_titlebar_left)
    ImageView activityTitlebarLeft;
    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.reg_act_user_phone)
    ClearEditText regActUserPhone;
    @BindView(R.id.address_update)
    Button addressUpdate;
    @BindView(R.id.code_weichat)
    ImageView codeWeichat;
    @BindView(R.id.activity_titlebar_right)
    ImageView activityTitlebarRight;
    @BindView(R.id.down_code_ll)
    LinearLayout downCodeLl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_center);
        ButterKnife.bind(this);
        activityTitlebarCenter.setText("客服中心");
        getServiceCode();
    }

    @OnClick({R.id.activity_titlebar_left, R.id.address_update,R.id.down_code_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_titlebar_left:
                finish();
                break;
            case R.id.address_update:
                postPhoneCall();
                break;
            case R.id.down_code_ll:
                codeWeichat.setDrawingCacheEnabled(true);
                Bitmap bitmap=codeWeichat.getDrawingCache();
                FileUtil.saveImageToGallery(this, bitmap,
                        Constants.RENT_CODE_PIC);
                codeWeichat.setDrawingCacheEnabled(false);
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void postPhoneCall() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", MyPerference.getInstance().getUserId());
        map.put("phone", regActUserPhone.getText().toString());
        HttpManager.getInstance().perform_post(API.SUB_SERVICE, map, new MyStringResponse(this) {
            @Override
            public void onError(Object tag, Throwable e) {
                Toast.makeText(CallCenterActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                CodeModel codeModel = new Gson().fromJson(response, CodeModel.class);
                if (0 == codeModel.getErrCode()) {
                    Toast.makeText(CallCenterActivity.this, "请保持手机畅通，客服稍后会联系您", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        });
    }
    private void getServiceCode(){
        HttpManager.getInstance().get(API.APP_CONFIG, new HashMap<String, Object>(), new MyStringResponse(this) {
            @Override
            public void onError(Object tag, Throwable e) {

            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                ServiceBean serviceBean=new Gson().fromJson(response,ServiceBean.class);
                if(serviceBean!=null&&serviceBean.getErrCode()==0){
                    if(!TextUtils.isEmpty(serviceBean.getData().getQrcode())){
                        ShowImageUtils.showImageView(CallCenterActivity.this,codeWeichat,serviceBean.getData().getQrcode(),R.drawable.service_code);
                    }
                }
            }
        });
    }
}
